import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


public class AddNewCourseTest extends AbstractTestCase {

	private static final CourseFactory cf = CourseFactory.instance;
	private static final UniManager um = UniManager.instance;
	
	private Date now, yesterday, tomorrow, day_after_tomorrow;
	
	public void setUp() throws Exception {
		Calendar cal = new GregorianCalendar();
		cal.setTime(new Date());
		
		now = cal.getTime();

		cal.add(Calendar.DAY_OF_YEAR, -1);
		yesterday = cal.getTime();
		
		cal.add(Calendar.DAY_OF_YEAR, 2);
		tomorrow = cal.getTime();
		
		cal.add(Calendar.DAY_OF_YEAR, 1);
		day_after_tomorrow = cal.getTime();
	}

	public void tearDown() throws Exception {
	}

	public void test() throws Exception {
		this.setUp();
		System.out.println("--------------------------------------");
		System.out.println("Start of AddNewCourseTest: " + TIME_FORMATTER.format(new Date()));
		System.out.println();
		
		testCourse("testCourse1", now, tomorrow, day_after_tomorrow, 2);
		testCourse("testCourse2", yesterday, now, yesterday);
		testCourse("testCourse3", now, yesterday, now);
		
		System.out.println("End of AddNewCourseTest: " + TIME_FORMATTER.format(new Date()));
		System.out.println("--------------------------------------");
		this.tearDown();
	}
	
	private void testCourse(String name, Date registerStart, Date registerEnd, Date unregisterEnd) {
		testCourse(name, registerStart, registerEnd, unregisterEnd, null);
	}
	
	private void testCourse(String name, Date registerStart, Date registerEnd, Date unregisterEnd, Integer numberOfInserts) {
		if(numberOfInserts == null || numberOfInserts < 1) {
			numberOfInserts = 1;
		}
		
		System.out.println("Create course: " + getCourseDetails(name, registerStart, registerEnd, unregisterEnd));
		System.out.println("CourseList size: " + um.getCourseListSize());
		
		try {
			Course c = cf.createCourse(name, registerStart, registerEnd, unregisterEnd);
			if(c != null) {
				System.out.println("Course successfully created");
			} else {
				throw new NullPointerException("Course is null");
			}
			for(int i = 0; i < numberOfInserts; i++) {
				um.addCourse(c);
			}
			System.out.println("Course added to CourseList. CourseList size: " + um.getCourseListSize());
		} catch (Exception e) {
			System.out.println("Error occured when creating Course: " + e.getMessage());
		}
		System.out.println("");
	}
	
	public static String getCourseDetails(Course c) {
		return getCourseDetails(c.getCourseName(), c.getRegisterStart(), c.getRegisterEnd(), c.getUnregisterEnd());
	}
	
	public static String getCourseDetails(String name, Date registerStart, Date registerEnd, Date unregisterEnd) {
		StringBuilder sb = new StringBuilder();
		
		sb.append(name);
		sb.append(" ");
		sb.append(DATE_FORMATTER.format(registerStart));
		sb.append(" ");
		sb.append(DATE_FORMATTER.format(registerEnd));
		sb.append(" ");
		sb.append(DATE_FORMATTER.format(unregisterEnd));
		
		return sb.toString();
	}
}
